/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "list物体插座.h"

#include <ppl.h>

using namespace std;

//#include "list元素插座.h"
#include "list数值插座.h"
//#include "list数值插座.h"

#include "../C_粒子插座.h"
#include "../通用插座.h"



#include "字符串转换.h"












void f_socket_resize物体插座数据(std::vector<S_物体*>& data, uint32 num) {
	if (data.size() > num) {
		for (uint32 i = num; i < data.size(); ++i) {
			if (data[i]->m_DataNum) {
				for (uint8 offset = 0; offset < data[i]->m_DataNum; ++offset) {
					f_surface_释放网格((S_Mesh*)data[i]->m_Data);
				}
			}
		}
	}

	data.resize(num);
}









C_插座_物体Array1D::C_插座_物体Array1D(wstring name) :C_插座基类(name, E_值类型::e_Type_Object_Array1D) {
	//m_ObjectNames = 0;
}

C_插座_物体Array1D::~C_插座_物体Array1D() {
	
}

void* C_插座_物体Array1D::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {
			return dynamic_cast<C_插座_物体Array1D*>(socket)->f_getData(当前插座连接线ID);

		}
		else if (socket->m_Type == E_值类型::e_Type_Object_1D) {
			auto* data = DEF_物体插座_1D数据(socket, 当前插座连接线ID);
			m_Value.resize(data->count);
			for (uint32 i = 0; i < data->count; ++i) {
				m_Value[i] = (data->ptr_userData[i]);
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* data = DEF_物体插座数据(socket, 当前插座连接线ID);
			m_Value.clear();
			m_Value.push_back(data);
		} 
		else if (socket->m_Type == E_值类型::e_Type_ParticleSystem) {
			//auto& data = *DEF_粒子系统插座Value(socket);
			//uint32 num = data.m_loc->size();
			
			//f_创建物体组(m_Value, num);
			//f_设置物体ID(m_Value, 0);
		} else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			//auto* data = DEF_转换为引用数据(socket);
			//if (data->m_Type == E_插座类型::e_Type_Object) {
			//	m_Value.push_back((S_物体数据*)data->m_Data);
			//}
			//else if (data->m_Type == E_值类型::e_Type_Object_Array1D) {
			//	auto& a = *((vector<S_物体数据*>*)data->m_Data);
			//	m_Value.insert(m_Value.end(), a.begin(), a.end());
			//}
		} 
		else if (socket->m_Type == E_值类型::e_Type_骨骼) {
			//auto& data = *DEF_粒子系统插座Value(socket);
			//uint32 num = data.m_loc->size();

			//f_创建物体组(m_Value, num);
			//f_设置物体ID(m_Value, 0);
		}
		else if (socket->m_Type == E_值类型::e_Type_骨架) {
			m_Value.clear();
			//m_Value.push_back(socket->f_getData(当前插座连接线ID));
		}
		else {
			m_Value.clear();
		}
	}
	return &m_Value;
}

void C_插座_物体Array1D::f_setData(void* data, uint8 线程ID) {
	if (m_是否为输出插座) {
		m_Value = *((vector<S_物体*>*)data);
	}
	else {
		cout << "错误操作: 往输入插座里设置数据" << endl;
	}
}

void C_插座_物体Array1D::f_setData(S_引用插座数据映射* data)
{
}


void C_插座_物体Array1D::f_释放数据() {
	m_Value.clear();
}











void f_设置物体ID(vector<S_物体*>& 物体, vector<int32>& ID, uint32 偏移) {
	uint32 num = 物体.size();
	ID.resize(num);

	auto* p1 = 物体.data();
	auto* p2 = ID.data();

	Concurrency::parallel_for<uint32>(0, num, [&](uint32 i) {
		p1[i]->m_ID = i + 偏移;
		p2[i] = i + 偏移;
	});
}

void f_设置物体ID(vector<S_物体*>& 物体, uint32 偏移) {
	uint32 num = 物体.size();
	auto* p1 = 物体.data();

	Concurrency::parallel_for<uint32>(0, num, [&](uint32 i) {
		p1[i]->m_ID = i + 偏移;
	});
}








C_物体插座_1D::C_物体插座_1D(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Object_1D) {
	m_默认值 = (S_OBArray*)f_core_array_new(0, sizeof(S_物体*));
	m_转换值 = (S_OBArray*)f_core_array_new(0, sizeof(S_物体*));
	m_Value = m_默认值;
}

C_物体插座_1D::~C_物体插座_1D() {
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
}

void* C_物体插座_1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Object_1D) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {
			std::vector<S_物体*> objs = *DEF_物体插座_Array1D数据(socket, 线程ID);

			uint32 num = objs.size();
			f_core_array_resize((S_Array*)m_转换值, num);
			
			for (uint32 i = 0; i < num; ++i) {
				m_转换值->ptr_userData[i] = objs[i];
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* data = DEF_物体插座数据(socket, 0);
			if (data) {
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
			}
			else {
				f_core_array_resize((S_Array*)m_转换值, 0);
			}
		}
		
		m_Value = m_转换值;
	}
	else {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_物体插座_1D::f_setData(void* data, uint8 线程ID) {
	if (data) {
		f_core_array_copy((S_Array*)m_默认值, (S_Array*)data);
	}
	else {
		f_core_array_resize((S_Array*)m_默认值, 0);
	}
	
	//m_Value = (S_OBArray*)data;
	//m_默认值 = m_Value;
}

bool C_物体插座_1D::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Object:
	case E_值类型::e_Type_Object_1D:
	case E_值类型::e_Type_Array:
	case E_值类型::e_插座Type_引用数据:
		return true;
	}
	return false;
}















C_插座_对象1D::C_插座_对象1D(wstring name) :C_插座基类(name, E_值类型::e_插座Type_根对象_1D) {
	//m_ObjectNames = 0;
}

C_插座_对象1D::~C_插座_对象1D() {
}

void* C_插座_对象1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_根对象_1D) {
			return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {
			std::vector<S_物体*> objs = *DEF_物体插座_Array1D数据(socket, 线程ID);

			m_Value.reserve(objs.size());
			for (auto& ob : objs) m_Value.push_back(ob);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* data = DEF_物体插座数据(socket, 0);
			//m_Value.clear();
			m_Value.push_back(data);
		}
		else if (socket->m_Type == E_值类型::e_Type_ParticleSystem) {
			//auto& data = *DEF_粒子系统插座Value(socket);
			//uint32 num = data.m_loc->size();

			//f_创建物体组(m_Value, num);
			//f_设置物体ID(m_Value, 0);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			//auto* data = DEF_转换为引用数据(socket);
			//if (data->m_Type == E_插座类型::e_Type_Object) {
			//	m_Value.push_back((S_物体数据*)data->m_Data);
			//}
			//else if (data->m_Type == E_值类型::e_Type_Object_Array1D) {
			//	auto& a = *((vector<S_物体数据*>*)data->m_Data);
			//	m_Value.insert(m_Value.end(), a.begin(), a.end());
			//}
		}
		else {
			m_Value.clear();
		}
	}
	return &m_Value;
}

void C_插座_对象1D::f_setData(void* data, uint8 线程ID) {
	if (m_是否为输出插座) {
		m_Value = *((vector<S_Object*>*)data);
	}
	else {
		cout << "错误操作: 往输入插座里设置数据" << endl;
	}
}

void C_插座_对象1D::f_setData(S_引用插座数据映射* data) {

}


void C_插座_对象1D::f_释放数据() {
	m_Value.clear();
}













C_网格1D插座::C_网格1D插座(wstring name) :C_插座基类(name, E_值类型::e_Type_Mesh_1D) {
	m_默认 = (S_MeshArray*)f_core_array_new(0, sizeof(S_Mesh*));
	m_转换 = (S_MeshArray*)f_core_array_new(0, sizeof(S_Mesh*));
	m_Value = m_默认;
}

C_网格1D插座::~C_网格1D插座() {
}

void* C_网格1D插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Mesh_1D) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* obj = DEF_物体插座数据(socket, 0);

			f_core_array_resize((S_Array*)m_转换, 1);
			m_转换->ptr_userData[0] = f_ob_fromMesh(obj);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_1D) {
			S_OBArray* obj = DEF_物体插座_1D数据(socket, 0);

			f_core_array_resize((S_Array*)m_转换, obj->count);
			for (uint32 i = 0; i < obj->count; ++i) {
				m_转换->ptr_userData[i] = f_ob_fromMesh(obj->ptr_userData[i]);
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {
			auto& objs = *DEF_物体插座_Array1D数据(socket, 线程ID);

			uint32 num = objs.size();
			auto* data = objs.data();
			f_core_array_resize((S_Array*)m_转换, num);
			for (uint32 i = 0; i < num; ++i) {
				m_转换->ptr_userData[i] = f_ob_fromMesh(data[i]);
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Mesh) {
			auto* data = DEF_网格插座数据(socket);
			f_core_array_resize((S_Array*)m_转换, 1);
			m_转换->ptr_userData[0] = data;
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto* data = DEF_引用插座数据(socket, 线程ID);
			if (data->m_Type == E_值类型::e_Type_Mesh_1D || data->m_Type == E_值类型::e_Type_Mesh) {
				return data;
			}
		}
		m_Value = m_转换;
	}
	else {
		m_Value = m_默认;
	}

	return m_Value;
}

void C_网格1D插座::f_setData(void* data, uint8 ID) {
	m_Value = (S_MeshArray*)data;
}








/*C_平面集插座::C_平面集插座(string name)
	:C_插座基类(name, E_插座类型::e_插座Type_物理平面_Array1D)
{

}

C_平面集插座::~C_平面集插座() {

}

void* C_平面集插座::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_插座类型::e_插座Type_物理平面_Array1D) {
			return dynamic_cast<C_平面集插座*>(socket)->f_getData();
		} else if (socket->m_Type == E_插座类型::e_Type_Object) {
			S_平面属性* data = DEF_平面属性插座Value(dynamic_cast<C_平面插座*>(socket));

			m_平面属性集.clear();
			m_平面属性集.push_back(*data);
		}
	}
	return &m_平面属性集;
}

void C_平面集插座::f_setData(void* data, int64 ID) {
	if (m_是否为输出插座) {
		m_平面属性集 = *((vector<S_平面属性>*)data);
	}
	else {
		cout << " C_平面集插座 - 错误操作: 往输入插座里设置数据" << endl;
	}
}





C_物理体集插座::C_物理体集插座(string name)
	:C_插座基类(name, E_插座类型::e_插座Type_物理体_Array1D)
{
}

C_物理体集插座::~C_物理体集插座()
{
}

void * C_物理体集插座::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_插座类型::e_插座Type_物理体_Array1D) {
			return dynamic_cast<C_物理体集插座*>(socket)->f_getData();
		}
		else if (socket->m_Type == E_插座类型::e_插座Type_物理体) {
			S_bl物理体对象包* data = DEF_物理体插座Value(dynamic_cast<C_物理体插座*>(socket));
			m_Value.clear();
			m_Value.push_back(*data);
		}
	}
	return &m_Value;
}

void C_物理体集插座::f_setData(void * data, int64 ID) {
	m_Value = *((vector<S_bl物理体对象包>*)data);
}





















/*
PyObject* f_构建PyArray1D网格数据(const vector<S_网格插座数据>& meshs) {
	uint32 num = meshs.size();
	PyObject* data = PyList_New(num*8);



#pragma loop(on_vector)
	for (uint32 i = 0; i < num; ++i) {
		PyObject* e = PyTuple_New(2);
		uint32 id = i * 8;

		PyTuple_SetItem(e, 0, f_string转PyObject("\n顶点数量:"));
		if (meshs[i].m_顶点) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_顶点->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+0, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n法线数量:"));
		if (meshs[i].m_法线) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_法线->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+1, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n面法线数量:"));
		if (meshs[i].m_面法线) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_面法线->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+2, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n面中心数量:"));
		if (meshs[i].m_面中心) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_面中心->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+3, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n边索引数量:"));
		if (meshs[i].m_边索引) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_边索引->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+4, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n面索数量:"));
		if (meshs[i].m_索引) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_索引->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+5, e);


		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n材质ID数量:"));
		if (meshs[i].m_材质ID) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_材质ID->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id+6, e);
		//PyList_SetItem(data, i, e);



		e = PyTuple_New(2);
		PyTuple_SetItem(e, 0, f_string转PyObject("\n材质槽数量:"));
		if (meshs[i].m_材质槽) PyTuple_SetItem(e, 1, Py_BuildValue("i", meshs[i].m_材质槽->size()));
		else PyTuple_SetItem(e, 1, Py_BuildValue(""));
		PyList_SetItem(data, id + 7, e);

	}

	
	PyObject* 头信息 = f_string转PyObject("============ " + f_整数值转宽字符串(num) + " 网格组 ==========");
	PyList_Insert(data, 0, 头信息);
	return data;
}














C_物体数据Array1D插座::C_物体数据Array1D插座(string name) :C_插座基类(name, E_插座类型::e_插座Type_ObjectData_Array1D)
{
}

C_物体数据Array1D插座::~C_物体数据Array1D插座() {

}

void* C_物体数据Array1D插座::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_插座类型::e_插座Type_ObjectData_Array1D) {
			return DEF_物体数据Array1DValue(socket);
		}
		else if (socket->m_Type == E_插座类型::e_插座Type_ObjectData) {
			//auto& objs = *DEF_物体Array1D插座Value(socket);

		}
		else if (socket->m_Type == E_插座类型::e_插座Type_引用数据) {
			auto* data = DEF_转换为引用数据(socket);
			if (data->m_Type == E_插座类型::e_插座Type_Mesh_Array1D || data->m_Type == E_插座类型::e_Type_Mesh) {
				return data;
			}
		}

	}
	return &m_Value;
}

void C_物体数据Array1D插座::f_setData(void* data, int64 ID) {

}

PyObject* C_物体数据Array1D插座::f_输出_bl数据(PyObject* bl_data) {
	return nullptr;
}















C_字体插座_1D::C_字体插座_1D(string name) :C_插座基类(name, E_插座类型::e_插座Type_字体_1D)
{
}

C_字体插座_1D::~C_字体插座_1D()
{
}

void* C_字体插座_1D::f_getData() {
	return nullptr;
}

void C_字体插座_1D::f_setData(void* data, int64 ID)
{
}

PyObject* C_字体插座_1D::f_输出_bl数据(PyObject* bl_data) {
	PyObject* data = PyList_New(0);

	return data;
}












C_材质插座_Array1D::C_材质插座_Array1D(string name) :C_插座基类(name, E_插座类型::e_插座Type_材质_Array1D)
{
}

C_材质插座_Array1D::~C_材质插座_Array1D()
{
}

void* C_材质插座_Array1D::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_插座类型::e_插座Type_材质_Array1D) {
			return dynamic_cast<C_材质插座_Array1D*>(socket)->f_getData();
		}
	}
	return &m_Value;
}

void C_材质插座_Array1D::f_setData(void* data, int64 ID) {
	m_Value = *((vector<S_材质插座数据>*)data);
}

PyObject* C_材质插座_Array1D::f_bl_传入数据(PyObject* bl_data) {
	uint32 num = PyList_Size(bl_data);
	if (num <= 0) return 0;

	auto& data = *DEF_材质Array1D插座Value(this);
	data.resize(num);
	auto* p = data.data();

	for (uint32 i = 0; i < num; ++i) {
		auto* e = PyList_GetItem(bl_data, i);
		p[i].mat = PyList_GetItem(e, 0);
		p[i].name = f_Py字符串转string(PyList_GetItem(e, 1));

		//Py_IncRef(p[i].mat);
	}
	return nullptr;
}

PyObject* C_材质插座_Array1D::f_输出_bl数据(PyObject* bl_data) {
	auto& data = *DEF_材质Array1D插座Value(this);
	uint32 num = data.size();

	auto* p = data.data();

	PyObject* r = PyList_New(num);
	for (uint32 i = 0; i < num; ++i) {
		auto* e = PyList_New(2);
		PyList_SetItem(e, 0, p[i].mat);
		PyList_SetItem(e, 1, p[i].mat);

		PyList_SetItem(r, i, e);
	}

	return r;
}
*/






C_骨骼1D插座::C_骨骼1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_插座Type_材质_Array1D) {
	//f_ob_创建骨骼(nullptr);
	m_是否单输入插座 = false;
}

C_骨骼1D插座::~C_骨骼1D插座() {
}

void* C_骨骼1D插座::f_getData(uint8 线程ID) {
	uint16 num = f_getLinkNum();

	m_Value.clear();
	for (uint16 i = 0; i < num; ++i) {
		C_插座基类* socket = f_getLinkOutSocket(i);
		if (socket) {
			if (socket->m_Type == E_值类型::e_Type_骨骼_Array1D) {
				auto& obs = *DEF_骨骼_1D数据(socket, i);
				m_Value.insert(m_Value.end(), obs.begin(), m_Value.end());
			}
			else if (socket->m_Type == E_值类型::e_Type_骨骼) {
				m_Value.push_back(DEF_物体插座数据(socket, i));
			}
			else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {

			}
			else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {

			}
		}
	}

	return &m_Value;
}

void C_骨骼1D插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((std::vector<S_物体*>*)data);
}

void C_骨骼1D插座::f_释放数据() {
}






